//
//  bubblecell.m
//  ONEChat
//
//  Created by 楼楚 on 16/1/14.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "bubblecell.h"

@implementation bubblecell
//泡泡view
- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf
{
    UIView *returnView = [self assembleMessageAtIndex:text from:fromSelf];
    returnView.backgroundColor = [UIColor clearColor];
    UIView *cellView = [[UIView alloc]initWithFrame:CGRectZero];
    cellView.backgroundColor = [UIColor clearColor];
    
    UIImage *bubble =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"bubbleSelf":@"bubble" ofType:@"png"]];
    UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:20 topCapHeight:14]];
    
    UIImageView *headImageView = [[UIImageView alloc] init];
    if (fromSelf) {
        [headImageView setImage:[UIImage imageNamed:@"face_test.png"]];
        returnView.frame= CGRectMake(9.0f, 15.0f, returnView.frame.size.width, returnView.frame.size.height);
        bubbleImageView.frame = CGRectMake(0.0f, 14.0f, returnView.frame.size.width+24.0f, returnView.frame.size.height+24.0f );
        cellView.frame = CGRectMake(265.0f-bubbleImageView.frame.size.width, 0.0f,bubbleImageView.frame.size.width+50.0f, bubbleImageView.frame.size.height+30.0f);
        headImageView.frame = CGRectMake(bubbleImageView.frame.size.width, cellView.frame.size.height-50.0f, 50.0f, 50.0f);

    }else{
        [headImageView setImage:[UIImage imageNamed:@"default_head_online.png"]];
        returnView.frame= CGRectMake(65.0f, 15.0f, returnView.frame.size.width, returnView.frame.size.height);
        bubbleImageView.frame = CGRectMake(50.0f, 14.0f, returnView.frame.size.width+24.0f, returnView.frame.size.height+24.0f);
        cellView.frame = CGRectMake(0.0f, 0.0f, bubbleImageView.frame.size.width+30.0f,bubbleImageView.frame.size.height+30.0f);
        headImageView.frame = CGRectMake(0.0f, cellView.frame.size.height-50.0f, 50.0f, 50.0f);
    }

    [cellView addSubview:bubbleImageView];
    [cellView addSubview:headImageView];
    [cellView addSubview:returnView];
//    [bubbleImageView release];
//    [returnView release];
//    [headImageView release];

    return cellView;
    
}

#define BEGIN_FLAG @"[/"
#define END_FLAG @"]"
//图文混排
-(void)getImageRange:(NSString*)message : (NSMutableArray*)array
{
    NSRange range = [message rangeOfString:BEGIN_FLAG];
    NSRange range1 = [message rangeOfString:END_FLAG];
    
    if (range.length>0 && range1.length>0) {
        if (range.location > 0) {
            [array addObject:[message substringToIndex:range.location]];
            [array addObject:[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)]];
            NSString *str=[message substringFromIndex:range1.location+1];
            [self getImageRange:str :array];
        }else {
            NSString *nextstr=[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)];
                if (![nextstr isEqualToString:@""]) {
                [array addObject:nextstr];
                NSString *str=[message substringFromIndex:range1.location+1];
                [self getImageRange:str :array];
            }else {
                return;
            }
        }
        
    } else if (message != nil) {
        [array addObject:message];
    }
}





//信息集成
#define KFacialSizeWidth  18
#define KFacialSizeHeight 18
#define MAX_WIDTH 150
-(UIView *)assembleMessageAtIndex : (NSString *) message from: (BOOL)fromself
{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    [self getImageRange:message :array];
    
    UIView *returnView = [[UIView alloc]initWithFrame:CGRectZero];
    
    NSArray *data = array;
    UIFont *fon = [UIFont systemFontOfSize:13.0f];
    CGFloat upX = 0;
    CGFloat upY = 0;
    CGFloat X = 0;
    CGFloat Y = 0;
    if (data) {
        for (int i = 0; i < [data count]; i ++) {
            NSString *str = [data objectAtIndex:i];
            NSLog(@"str--->%@",str);
            if ([str hasPrefix:BEGIN_FLAG]&&[str hasSuffix:END_FLAG]) {
                if (upX >=MAX_WIDTH) {
                    upY = upY + KFacialSizeHeight;
                    upX = 0;
                    X = 150;
                    Y = upY;
                }
                NSLog(@"str(image)---->%@",str);
                NSString *imageName = [str substringWithRange:NSMakeRange(2, str.length - 3)];
                UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
                imgView.frame = CGRectMake(upX, upY, KFacialSizeWidth, KFacialSizeHeight);
                [returnView addSubview:imgView];
//                [imgView release];
                upX=KFacialSizeWidth+upX;
                if (X<150) X = upX;
            }else{
                for (int j = 0; j <[str length]; j ++) {
                    NSString *temp = [str substringWithRange:NSMakeRange(j, 1)];
                    if (upX >=MAX_WIDTH) {
                        upY = upY + KFacialSizeHeight;
                        upX = 0;
                        X = 150;
                        Y = upY;
                    }
                    CGSize size=[temp sizeWithFont:fon constrainedToSize:CGSizeMake(150, 40)];
                    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(upX,upY,size.width,size.height)];
                    la.font = fon;
                    la.text = temp;
                    la.backgroundColor = [UIColor clearColor];
                    [returnView addSubview:la];
//                    [la release];
                    upX=upX+size.width;
                    if (X<150) {
                        X = upX;
                    }
                }
            }
        }
    }
    
    returnView.frame = CGRectMake(15.0f,1.0f, X, Y);
    
    NSLog(@"%.1f %.1f", X, Y);
    return returnView;
    
    
}


@end
